// pages/order/order-pre/order-pre.js
var httpUtil = require('../../../../utils/httpUtil.js');
var uiUtil = require('../../../../utils/uiUtil.js');
var WXBizDataCrypt = require('../../../../utils/cryptoUtil.js');
const appId = 'wx2e65f1eb244a839b';
const app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCartLists: [],//购物车列表
    mchHome: null, //商家信息
    tablewareNum: ["无需餐具", "1份", "2份", "3份", "4份", "5份", "6份", "7份", "8份", "9份", "10份"],
    index:null,
    value:{
      site:null,
      noteText:null,
      invoice:null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    let mchHome = JSON.parse(options.mchHome);
    let shopCartLists = JSON.parse(options.shopCartLists);
    let site=app.globalData.userLocation.pois[0].site;
    
    that.setData({
      pages: app.globalData.pages,
      authorizeFlag:app.globalData.authorizeFlag,
      mchHome: mchHome,
      shopCartLists: shopCartLists,
      "value.site":site
    });
  },


  /**
   * 选择餐具数量
   */
  bindTablewareNumChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },


  /**
   * 提交订单校验
   */
  getSaveOrder(e){
    let value = that.data.value;
    if (value.site==""){
      uiUtil.showToast("请选择收货地址");
    } else if (value.invoice==""){
      uiUtil.showToast("请选择发票信息");
    } else {
      that.getOrderPay(e);
    }
  },


  /**
   * 支付
   */
  getOrderPay(e){
    let currentPrice = e.currentTarget.dataset.currentprice;
    let mchHome = that.data.mchHome;
    let value = that.data.value;
    let tablewareNum = that.data.tablewareNum;
    let index = that.data.index;
    var bill_id=0;
    var is_bill=0;
    if (value.invoice!=null){
      if (value.invoice.id){
        bill_id = value.invoice.id;
        is_bill = 1;
      }
    }
    
    if (value.site==null){
      uiUtil.showToast("请选择收货地址");
      return
    }
    

    httpUtil.PostRequest("saveOrder", {
      mchId: mchHome.mchId,
      receiverName: value.site.name,
      receiverPhone: value.site.phone,
      receiverAddress: value.site.area + value.site.address,
      remark: value.noteText,
      tablewareNum: index,
      is_bill: is_bill,
      bill_id: bill_id
    }, {
        success(res) {
          var pages = getCurrentPages();
          var lastPage = pages[0];
          var lastPageShop=pages[pages.length - 2];
          if (lastPage.route == "pages/index/navigationbar/navigationbar") {
            lastPage.refresherrefreshOrder();
          } 
          if(lastPageShop.route == "pages/shop/shop/shop"){
            lastPageShop.getShopCart(mchHome.mchId);
          }
          wx.redirectTo({
            url: '/pages/pay/pay?mchkey=' + that.data.mchHome.mchKey + "&orderNo=" + res.orderNo + "&currentPrice=" + currentPrice
          })
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },


  /**
   * 点击立即充值，获取用户手机号
   */
  getphonenumber (e) {
    if(e.detail.errMsg=="getUserInfo:fail auth deny"){ //用户拒绝授权获取用户手机号
      return
    }

    var sessionKey = app.globalData.sessionKey;
    var encryptedData = e.detail.encryptedData;
    var iv =  e.detail.iv;

    var pc = new WXBizDataCrypt(appId, sessionKey);
    var data = pc.decryptData(encryptedData , iv);
    app.globalData.userPhone=data;

    // let currentPrice = e.currentTarget.dataset.currentprice;
    that.getUser(e);
  },


  /**
   * 提交用户信息
   */
  getUser: function (e){
    let userPhone=app.globalData.userPhone;
    let userInfo=app.globalData.userInfo;
    httpUtil.PostRequest("update_small_user", {
      wechantName: userInfo.nickName,
      phone: userPhone.purePhoneNumber,
      avatarUrl: userInfo.avatarUrl,
      unionId: userInfo.unionId
    }, {
      success(res) {
        app.globalData.authorizeFlag=true;
        that.setData({
          authorizeFlag:true
        })
        that.getOrderPay(e);
      },
      fail(code, res) {
        uiUtil.showToast(res);
      }
    })
  },

  
})